package micro.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import micro.data.model.User;
import micro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author txw
 * @version V1.0
 * @Title: UserController
 * @ProjectName micro-user-server
 * @Description: 描述
 * @since 2018/6/18 23:41
 */
@RestController
@RequestMapping("/micro/user/")
@Api("用户服务")
public class UserController {

    private final UserService userService;

    /**
     * 保证线程安全
     * @param userService
     */
    @Autowired
    public UserController(UserService userService){
        this.userService = userService;
    }
    /**
     * 登录
     * @param name
     * @param pwd
     * @return
     */
    @GetMapping("login")
    @ApiOperation("登录")
    public User login(HttpServletRequest request,String name,String pwd){
        HttpSession session=request.getSession();
        User user=userService.login(name,pwd);
        session.setAttribute("user",user);
        return user;
    }

    /**
     * 查询id
     * @param id
     * @return
     */
    @GetMapping("getId/{id}")
    @ApiOperation("查询id")
    public User findById(HttpServletRequest request,@PathVariable("id") Long id){
        return userService.findByOne(id);
    }
    @GetMapping("getSession")
    @ApiOperation("获取session")
    public User findById(HttpServletRequest request){
        HttpSession session=request.getSession();
        return (User)session.getAttribute("user");
    }
}
